package yeliuhuishi.hadoopSerialization;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import yeliuhuishi.File.DelAllFile;

import java.io.File;
import java.io.IOException;


/**
 * 统计用户上下流量
 * sum
 */
public class FlowCount {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        String input = "src/main/java/yeliuhuishi/hadoopSerialization/input";
        String output = "src/main/java/yeliuhuishi/hadoopSerialization/output";
        DelAllFile.delAllFile(new File(output));
        //创建配置对象
        Configuration conf = new Configuration();
        //conf.set("mapred.job.tracker", "Centos7:9000");
        //conf.set("fs.defaultFS","hdfs://Centos7:9000");
        //创建Job对象
        Job job = Job.getInstance(conf, "FlowCount");
        //设置mapper类
        job.setMapperClass(FlowCountMapper.class);
        //设置 Reduce类
        job.setReducerClass(FlowCountReduce.class);

        //设置运行job类
        job.setJarByClass(FlowCount.class);

        //设置map输出的key,value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBean.class);
        //设置reduce输出的key,value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //设置输入路径金额输出路径
        FileInputFormat.setInputPaths(job, new Path(input));//input
        //设置输出路径
        FileOutputFormat.setOutputPath(job, new Path(output));//output
        //  提交
        //  没有返回值,不知道是否成功,所以不用
        //  job.submit();

        System.out.println(job.waitForCompletion(true) ? "成功" : "失败");
    }

    public static class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] fields = value.toString().split(",");

            String phone = fields[0];
            long upFlow = Long.parseLong(fields[1]);
            long dFlow = Long.parseLong(fields[2]);
            context.write(new Text(phone), new FlowBean(upFlow, dFlow));
        }
    }

    public static class FlowCountReduce extends Reducer<Text, FlowBean, Text, FlowBean> {

        @Override
        protected void reduce(Text key, Iterable<FlowBean> values, Context context)
                throws IOException, InterruptedException {

            long sumUpFlow = 0L;
            long sumDFlow = 0L;
            for (FlowBean bean : values) {
                sumUpFlow += bean.getUpFlow();
                sumDFlow += bean.getdFlow();
            }

            FlowBean sumBean = new FlowBean(sumUpFlow, sumDFlow);
            context.write(key, sumBean);
        }
    }
}
